import time

import pytest as pytest
from appium import webdriver
import pytest
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


@pytest.fixture(scope="session", autouse=True)
def init_driver():
    caps = {"platformName": "Android",
            "deviceName": "127.0.0.1:62001",
            "appPackage": "com.cnezsoft.xxm",
            "appActivity": ".MainActivity",
            "noReset": True
            }

    driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
    driver.implicitly_wait(4)
    return driver


class Test:
    # 点击右上角"+"号
    def test_1(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "创建聊天").click()
        time.sleep(1)

    # 点击成员-创建
    def test_2(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "W\nwjw01").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "创建").click()
        time.sleep(1)

    # 选中成员并发送消息
    def test_3(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "W\nwjw01\n4月26日\n[图片]").click()
        time.sleep(1)
        driver.find_element(AppiumBy.__text_signature__, "发送消息").send_keys("在吗？")
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "发送").click()
        time.sleep(1)

    # 发送其他内容(照片/文件/拍照)
    def test_4(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "W\nwjw01\n4月26日\n[图片]").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "发送其他内容").click()
        time.sleep(1)

    # 收藏联系人
    def test_5(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "W\nwjw01\n4月26日\n[图片]").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "显示菜单").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "收藏").click()
        time.sleep(1)

    # 查看联系人资料
    def test_6(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "W\nwjw01\n4月26日\n[图片]").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "显示菜单").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "资料").click()
        time.sleep(1)

    # 搜索功能
    def test_7(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "搜素").send_keys("wjw01")
        time.sleep(1)

    # 打开通讯录，进入讨论组
    def test_8(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "通讯录\n通讯录\n第 2 个标签，共 3 个").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "讨论组").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "系统").click()
        time.sleep(1)

    # 在讨论组发送@全体成员的消息
    def test_9(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "通讯录\n通讯录\n第 2 个标签，共 3 个").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "讨论组").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "系统").click()
        time.sleep(1)
        driver.find_element(AppiumBy.__text_signature__, "发送消息").send_keys("@")
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "@所有人").click()
        time.sleep(1)
        driver.find_element(AppiumBy.XPATH, "//android.widget.Button[@content-desc='发送']").click()

    # 更改讨论组名称
    def test_10(self, init_driver):
        driver = init_driver
        time.sleep(2)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "通讯录\n通讯录\n第 2 个标签，共 3 个").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "讨论组").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "系统").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "显示菜单").click()
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "重命名").click()
        time.sleep(1)
        driver.find_element(AppiumBy.XPATH, "//android.widget.EditText").send_keys("new name")
        time.sleep(1)
        driver.find_element(AppiumBy.ACCESSIBILITY_ID, "重命名").click()
        time.sleep(1)